package com.mark.controller.common;

/*
* @Author: Choisaaaa
* @Date: 2018/5/7  23:37
* 图片上传控制器
*/

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mark.common.ServerResponse;
import com.mark.controller.common.CurrentUser;
import com.mark.enums.PropertiesEnum;
import com.mark.pojo.User;
import com.mark.service.IFileService;
import com.mark.util.PropertiesUtil;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/image/")
public class ImgUploadController {
    @Autowired
    private IFileService iFileService;

    @RequestMapping(value = "upload.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse uploadImage(HttpServletRequest request){
        String role  = null;
        Integer roleId = null;
        User currentUser ;
        ServerResponse response = CurrentUser.getCurrentUser(request);
        if (!response.isSuccess()){
            //return response; todo ruguoshigualniyuan
            role = "user";
            roleId = 0;
        }else{
            currentUser = (User) response.getData();
            role = "user";
            roleId = currentUser.getId();
        }
        //定义解析器

        List<Map>  images = Lists.newArrayList();
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if(commonsMultipartResolver.isMultipart(request)){//判断请求中是否含有Multipart文件
            MultipartHttpServletRequest  multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            Iterator<String> iterator =  multipartHttpServletRequest.getFileNames();//获取上传文件的迭代器
            while (iterator.hasNext()){
                Map<String,Object> fileMap = Maps.newHashMap();
                MultipartFile multipartFile =  multipartHttpServletRequest.getFile(iterator.next());//根据文件名称获取文件流
                if(StringUtils.equals(multipartFile.getName(),"category")){
                    role = "category";
                }else if(StringUtils.equals(multipartFile.getName(),"headLine")){
                    role = "headLine";
                }
                String path = request.getSession().getServletContext().getRealPath("upload");
                String targetFileName = iFileService.upload(multipartFile,path,role ,roleId);
                String url = PropertiesUtil.getProperty(PropertiesEnum.FTP_SERVICE_HTTP_PREFIX.getPropertiesName())+targetFileName;
                fileMap.put("uri",targetFileName);
                fileMap.put("host",PropertiesUtil.getProperty(PropertiesEnum.FTP_SERVICE_HTTP_PREFIX.getPropertiesName()));
                fileMap.put("url",url);
                images.add(fileMap);
            }
        }
        return ServerResponse.createBySuccess(images);
    }

    //7.上传富文本中的图片
    @RequestMapping(value="richtext_img_upload" , method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> richtextImgUpload(@RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> resultMap = Maps.newHashMap();
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file, path,"simditor",0);
        if (org.apache.commons.lang3.StringUtils.isBlank(targetFileName)) {
            resultMap.put("success", false);
            resultMap.put("msg", "上传失败");
            return resultMap;
        }
        String url = PropertiesUtil.getProperty(PropertiesEnum.FTP_SERVICE_HTTP_PREFIX.getPropertiesName()) + targetFileName;
        resultMap.put("success", true);
        resultMap.put("msg", "上传成功");
        resultMap.put("file_path", url);
        //response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
        return resultMap;
    }
}
